#pragma once

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>
#include "poco/user.h"
#include "poco/word.h"
#include "poco/vocabulary.h"
#include "poco/communitypost.h"

namespace WordMemory
{
    class ServerHelper : public QObject
    {
        Q_OBJECT

    public:
        // 用户管理接口
        void login(const QString &username, const QString &password);
        void registerUser(const QString &username, const QString &password, const QString &realName);
        void getUser(int userId);
        void updateUser(int userId, const QString &realName);

        // 单词接口
        void getWord(int userId, const QString &word);
        void addCustomExample(int wordId, const QString &example, const QString &translation);
        void addCustomMnemonic(int wordId, const QString &mnemonic);
        void updateWordStatus(int userId, int wordId, const QString &status);
        void addWordToFavorites(int userId, int wordId);
        void removeWordFromFavorites(int userId, int wordId);
        void addWordToMistakesBook(int userId, int wordId);
        void removeWordFromMistakesBook(int userId, int wordId);

        // 词库接口
        void getVocabularies(int userId);
        void addVocabulary(int userId, const QString &name);
        void getWordsByVocabularyId(int userId, int vocabularyId);
        void getDefaultVocabulary(int userId);
        void setDefaultVocabulary(int userId, int vocabularyId);
        void getMistakesBookWords(int userId);

        // 学习统计接口
        void getLearningStats(int userId);
        void getDailyLearningSummary(int userId, int days = 30);

        // 社区接口
        void getCommunityPosts(int currentUserId = 0);
        void getCommunityPostDetails(int postId, int currentUserId = 0);
        void toggleLikePost(int postId, int userId);
        void recordSharePost(int postId);

    signals:
        // 用户管理信号
        void loginFinished(bool success, const User &user, const QString &error);
        void registerFinished(bool success, const User &user, const QString &error);
        void getUserFinished(bool success, const User &user, const QString &error);
        void updateUserFinished(bool success, const User &user, const QString &error);

        // 单词信号
        void getWordFinished(bool success, const Word &word, const QString &error);
        void addCustomExampleFinished(bool success, const QString &error);
        void addCustomMnemonicFinished(bool success, const QString &error);
        void updateWordStatusFinished(bool success, const QString &error);
        void addWordToFavoritesFinished(bool success, int wordId, const QString &error);
        void removeWordFromFavoritesFinished(bool success, int wordId, const QString &error);
        void addWordToMistakesBookFinished(bool success, int wordId, const QString &error);
        void removeWordFromMistakesBookFinished(bool success, int wordId, const QString &error);

        // 词库信号
        void getVocabulariesFinished(bool success, const QList<Vocabulary> &vocabularies, const QString &error);
        void addVocabularyFinished(bool success, const Vocabulary &vocabulary, const QString &error);
        void getWordsByVocabularyIdFinished(bool success, int vocabularyId, const QList<Word> &words, const QString &error);
        void getDefaultVocabularyFinished(bool success, const Vocabulary &vocabulary, const QString &error);
        void setDefaultVocabularyFinished(bool success, const QString &error);
        void getMistakesBookWordsFinished(bool success, const QList<Word>& words, const QString &error);

        // 学习统计信号
        void getLearningStatsFinished(bool success, const QJsonObject &stats, const QString &error);
        void getDailyLearningSummaryFinished(bool success, const QJsonArray &summary, const QString &error);

        // 社区信号
        void getCommunityPostsFinished(bool success, const QList<CommunityPost> &posts, const QString &error);
        void getCommunityPostDetailsFinished(bool success, const CommunityPost &post, const QString &error);
        void toggleLikePostFinished(bool success, int postId, int newLikesCount, bool isLikedByCurrentUser, const QString &error);
        void recordSharePostFinished(bool success, int postId, int newSharesCount, const QString &error);

    public:
        ServerHelper(QObject *parent = nullptr);
        ServerHelper(const ServerHelper &);
        ServerHelper &operator=(const ServerHelper &);

    private:
        void sendPutRequest(const QString &path, const QJsonObject &data, std::function<void(QNetworkReply *)> callback);
        void sendPostRequest(const QString &path, const QJsonObject &data, std::function<void(QNetworkReply *)> callback);
        void sendGetRequest(const QString &path, const QUrlQuery &query, std::function<void(QNetworkReply *)> callback);

        QNetworkAccessManager *manager;
        const QString baseUrl = "http://192.168.3.16:8080";
    };
}
